package com.mojo.webflux.controller;

import com.mojo.webflux.pojo.User;
import com.mojo.webflux.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 使用reactor核心接口来实现web开发示例
 *
 * @author <a href="mailto:mojo_jj@163.com">Mojo</a>
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("user/{id}")
    public Mono<User> findById(@PathVariable Integer id) {
        return userService.findById(id);
    }

    @GetMapping("user")
    public Flux<User> findAll() {
        return userService.findAll();
    }

    @PostMapping("user")
    public Mono<Void> add(@RequestBody User user) {
        Mono<User> userMono = Mono.justOrEmpty(user);
        return userService.add(userMono);
    }
}
